package com.haoze.nurse.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.nurse.model.PaymentNoticeEntity;
import com.haoze.nurse.service.QueryStatisticsService;
import com.haoze.nurse.vo.InOrOutHispotal;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 查询统计
 *
 * @Author: Chenchao
 * @CreateTime: 2020-12-11 09:38
 */
@RestController
@RequestMapping(value = "/queryStatistics")
public class QueryStatisticsController {
    @Autowired
    private QueryStatisticsService queryStatisticsService;

    @GetMapping("/paymentNoticeConfig")
    @ApiOperation("催款单设置")
    public Result paymentNoticeConfig(@RequestParam String wardId) throws Exception {
        List<PaymentNoticeEntity> list = queryStatisticsService.paymentNoticeConfig(wardId);
        return ResultGenerator.genOkResult(list);
    }

    @PostMapping("/savePaymentNotice")
    @ApiOperation("保存催款单")
    public Result savePaymentNotice(@RequestBody PaymentNoticeEntity paymentNotice) {
        return queryStatisticsService.savePaymentNotice(paymentNotice);
    }

    @GetMapping("/delPaymentNotice/{id}")
    @ApiOperation("删除催款单")
    public Result delPaymentNotice(@PathVariable String id) {
        return queryStatisticsService.delPaymentNotice(id);
    }

    @GetMapping("/queryPatientPaymentNotice")
    @ApiOperation("查询病区病人催款单")
    public Result queryPatientPaymentNotice(@RequestParam String wardId, @RequestParam(required = false) String queryString) {
        return queryStatisticsService.queryPatientPaymentNotice(wardId, queryString);
    }

    @GetMapping("/queryPatientInOrOutHospital")
    @ApiOperation("在/出院病人")
    public Result queryPatientInOrOutHospital(@RequestParam Integer status, @RequestParam String wardDeptId,
                                              @RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "20") Integer pageSize,
                                              @RequestParam(required = false) String outBeginDate,@RequestParam(required = false) String outEndDate) {
        return queryStatisticsService.queryPatientInOrOutHospital(status, wardDeptId,page,pageSize,outBeginDate,outEndDate);
    }

    @GetMapping("/ExportPatientInOrOutHospital")
    @ApiOperation("导出在/出院病人信息")
    public void ExportPatientInOrOutHospital(@RequestParam Integer status, @RequestParam String wardDeptId,
                                               @RequestParam(required = false) String outBeginDate, @RequestParam(required = false) String outEndDate, HttpServletRequest req, HttpServletResponse res) {
        PageInfo pageInfo=(PageInfo) queryStatisticsService.queryPatientInOrOutHospital(status, wardDeptId,1,2147483647,outBeginDate,outEndDate).getData();
        CopyOnWriteArrayList<Object> arrayList = new CopyOnWriteArrayList<>();
        List<InOrOutHispotal> list = pageInfo.getList();
        list.parallelStream().forEach(inOrOutHispotal -> arrayList.add(BeanUtil.beanToMap(inOrOutHispotal)));
        queryStatisticsService.exportPatientInOrOutHospital(status,arrayList,req,res);
    }
}
